package com.fjec.devops.service.impl;

import com.fjec.common.Const;
import com.fjec.common.util.DateUtil;
import com.fjec.devops.feign.MarketClient;
import com.fjec.common.protocol.JsonResult;
import com.fjec.devops.entity.StatMarketUserNum;
import com.fjec.devops.mapper.StatMarketUserNumMapper;
import com.fjec.devops.service.IStatMarketUserNumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.devops.view.MarketUserNumVO;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 015.统计-市场板块注册用户数表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-18
 */
@Service
public class StatMarketUserNumServiceImpl extends ServiceImpl<StatMarketUserNumMapper, StatMarketUserNum> implements IStatMarketUserNumService {

    @Resource
    private StatMarketUserNumMapper mapper;

    @Resource
    private MarketClient marketClient;

    @Override
    public JsonResult statFarmerSortingPkgUserNum(int days, Integer markCode) {
        List<Integer> typeList = Arrays.asList(new Integer[]{2, 3, 5});
        // 通过days计算开始日期和结束日期
        LocalDate endTime = DateUtil.nowLd();
        LocalDate startTime = DateUtil.ldMinus(endTime, ChronoUnit.DAYS, days);
        List<StatMarketUserNum> entityList = mapper.statShopUserNum(typeList, startTime, endTime, markCode);
        List<String> xList = new ArrayList<>();
        List<Integer> farmerList = new ArrayList<>();
        List<Integer> sortingList = new ArrayList<>();
        List<Integer> pkgList = new ArrayList<>();
        List<Integer> totalList = new ArrayList<>();
        // 统计各商户类型数据
        for (StatMarketUserNum entity : entityList) {
            String statDate = DateUtil.format(entity.getStatDate(), "MM月dd日");
            if (!xList.contains(statDate)) {
                xList.add(statDate);
            }
            switch (entity.getBusiType()) {
                case 2:
                    farmerList.add(entity.getUserNum());
                    break;
                case 3:
                    sortingList.add(entity.getUserNum());
                    break;
                case 5:
                    pkgList.add(entity.getUserNum());
                    break;
            }
        }
        // 不分类型，统计数据
        entityList = mapper.statShopUserNumTotal(startTime, endTime, markCode);
        for (StatMarketUserNum entity : entityList) {
            totalList.add(entity.getUserNum());
        }
        // 整理数据
        List<Map<String, Object>> dataList = new ArrayList<>();
        // 注册总数
        Map<String, Object> tmpMap = new HashMap<>();
        tmpMap.put("name", "注册总数");
        tmpMap.put("data", totalList);
        dataList.add(tmpMap);
        // 果农
        tmpMap = new HashMap<>();
        tmpMap.put("name", "果农");
        tmpMap.put("data", farmerList);
        dataList.add(tmpMap);
        // 分拣厂
        tmpMap = new HashMap<>();
        tmpMap.put("name", "分拣厂");
        tmpMap.put("data", sortingList);
        dataList.add(tmpMap);
        // 包装物厂
        tmpMap = new HashMap<>();
        tmpMap.put("name", "包装物厂");
        tmpMap.put("data", pkgList);
        dataList.add(tmpMap);
        // 返回数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("xlist", xList);
        resultMap.put("dataList", dataList);
        return JsonResult.success()
                .data(resultMap)
                .build();
    }

    @Override
    public JsonResult statMaterialUserNum(int days) {
        List<Integer> typeList = Arrays.asList(new Integer[]{4});
        // 通过days计算开始日期和结束日期
        LocalDate endTime = DateUtil.nowLd();
        LocalDate startTime = DateUtil.ldMinus(endTime, ChronoUnit.DAYS, days);
        List<StatMarketUserNum> entityList = mapper.statShopUserNum(typeList, startTime, endTime, Const.MARKET_CODE_MATERIAL);
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (StatMarketUserNum entity : entityList) {
            xList.add(DateUtil.format(entity.getStatDate(), "MM月dd日"));
            yList.add(entity.getUserNum());
        }
        MarketUserNumVO view = new MarketUserNumVO();
        view.setXList(xList);
        view.setYList(yList);
        return JsonResult.success()
                .data(view)
                .build();
    }

    @Override
    public JsonResult statPurchaseUserNum(int days) {
        List<Integer> typeList = Arrays.asList(new Integer[]{6});
        // 通过days计算开始日期和结束日期
        LocalDate endTime = DateUtil.nowLd();
        LocalDate startTime = DateUtil.ldMinus(endTime, ChronoUnit.DAYS, days);
        List<StatMarketUserNum> entityList = mapper.statShopUserNum(typeList, startTime, endTime, Const.MARKET_CODE_PURCHASE);
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (StatMarketUserNum entity : entityList) {
            xList.add(DateUtil.format(entity.getStatDate(), "MM月dd日"));
            yList.add(entity.getUserNum());
        }
        MarketUserNumVO view = new MarketUserNumVO();
        view.setXList(xList);
        view.setYList(yList);
        return JsonResult.success()
                .data(view)
                .build();
    }

    @Override
    public JsonResult statMarketUserTotalNum() {
        // RPC查询所有市场
        List<Map<String, Object>> marketList = marketClient.getAllMarket();
        // 统计数据
        List<StatMarketUserNum> entityList = mapper.statMarketUserTotalNum();
        List<Integer> codeList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (Map<String, Object> market : marketList) {
            int markCode = MapUtils.getIntValue(market, "MarkCode");
            String markName = MapUtils.getString(market, "MarkName");
            codeList.add(markCode);
            nameList.add(markName);
            Integer userNum = null;
            for (StatMarketUserNum entity : entityList) {
                if (markCode == entity.getMarkCode().intValue()) {
                    userNum = entity.getUserNum();
                }
            }
            if (userNum == null) {
                userNum = 0;
            }
            numList.add(userNum);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("name", nameList);
        resultMap.put("value", numList);
        resultMap.put("codes", codeList);
        return JsonResult.success()
                .data(resultMap)
                .build();
    }
}
